// pages/cart/cart.js
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabid: 2,
    checked: false,
    pic: '0.00',
    orderFoods:[]
  },
  onChange(event) {
    let page = this;
    let order = wx.getStorageSync('order');
    let foodId = event.target.dataset.foodid;
    let foodproce = event.target.dataset.foodproce;
    let count = event.detail;
    let param = {
        food_id:foodId,
        order_id:order.order_id,
        food_count:count,
        food_price:(foodproce*count)
    }
    if(count == 0){
      wx.request({
        url: 'http://test.com:3000/order/delOrderFood',
        method:'POST',
        data:param,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success:function(res){
          if(res.statusCode == 200){
            let datas = res.data.data;
            let pic = 0;
            for (let index = 0; index < datas.length; index++) {
              const element = datas[index];
              pic += element.order_food_price;
            }
            pic = pic.toFixed(2).toString();
            page.setData({
              orderFoods: datas,
              pic:pic
            });
          }
        }
      })
    }else{
      wx.request({
        url: 'http://test.com:3000/order/updataOrderFood',
        method:'POST',
        data:param,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success:function(res){
          if(res.statusCode == 200){
            let datas = res.data.data;
            let pic = 0;
            for (let index = 0; index < datas.length; index++) {
              const element = datas[index];
              pic += element.order_food_price;
            }
            pic = pic.toFixed(2).toString();
            page.setData({
              orderFoods: datas,
              pic:pic
            });
          }
        }
      })
    }
    
  },
  toMenu(event){
    wx.switchTab({
			url: "/pages/menu/menu",
		})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let page = this;
    
    this.getTabBar().setData({
			active: this.data.tabid
		})
  },
  submitOrder(){
    let order = wx.getStorageSync('order');
    let page = this;
    wx.request({
      url: 'http://test.com:3000/order/setOrder',
      method:'POST',
      data:order,
      success(res){
        if (res.statusCode == '200') {
          Toast.success('订单已提交，请等待！')
          wx.request({
            url: 'http://test.com:3000/order/orderFood?id='+order.order_id,
            method: 'get',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success:function(res){
              if(res.statusCode == 200){
                let datas = res.data.data;
                let pic = 0;
                for (let index = 0; index < datas.length; index++) {
                  const element = datas[index];
                  pic += element.order_food_price;
                }
                pic = pic.toFixed(2).toString();
                page.setData({
                  orderFoods: datas,
                  pic:pic
                });
              }
            }
          })
          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let order = wx.getStorageSync('order');
    if(!order){
      console.log(order);
      Toast.fail('请先排队，再点餐！');
      return;
    }
    let page = this;
    wx.request({
      url: 'http://test.com:3000/order/orderFood?id='+order.order_id,
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        if(res.statusCode == 200){
          let datas = res.data.data;
          let pic = 0; //总价
          for (let index = 0; index < datas.length; index++) {
            const element = datas[index];
            pic += element.order_food_price;
          }
          pic = pic.toFixed(2).toString();
          console.log(datas);
          page.setData({
            orderFoods: datas,
            pic:pic
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})